//
//  SuggestViewController.m
//  Cat
//
//  Created by lushitong on 2018/12/1.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "SuggestViewController.h"
#import "HttpUserManager.h"
#import "HXTagsView.h"

@interface SuggestViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *feedTextView;
@property (nonatomic, strong) UIButton *bottomButton;
@property (nonatomic, copy) NSString *contextString;

@end

@implementation SuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.topTitleView setDetailDic:@{Nav_Title:@"意见反馈"}.mutableCopy];
	[self loadAllSubViews];
}

- (void)sendFeedBack {
	NSString *text = [self.contextString trimmingSartEndWhitespace];
	if (text.length == 0) {
		self.feedTextView = @"";
		[FLProgressHUD showMessageCenter:@"请输入反馈信息"];
		return;
	}
	[HttpUserManager senFeedBackWithContext:text withSuccuss:^(NSDictionary * _Nonnull dict) {
		[FLProgressHUD showSuccess:@"我们已经收到了你的反馈"];
		[self.navigationController popViewControllerAnimated:YES];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

#pragma mark----- delegaet -----

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
	if ([text isEqualToString:@"\n"]) {
		//禁止输入换行
		return NO;
	}
	return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
	if (textView.markedTextRange == nil) {
		self.contextString = textView.text;
		QKLog(@"text:%@", textView.text);
	}
}

- (void)loadAllSubViews {
	[self.view addSubviews:@[self.feedTextView,self.bottomButton]];
	
	[self.feedTextView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(16));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(16));
		make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(16));
		make.height.equalTo(@(kQKFitModuleDecimal(267)));
	}];
	
	[self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.bottom.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(50)));
	}];
}

#pragma mark----- lazy init -----

- (UITextView *)feedTextView {
	if (!_feedTextView) {
		_feedTextView = [[UITextView alloc] init];
		_feedTextView.layer.cornerRadius = kQKFitModuleDecimal(3);
		_feedTextView.placeholder = @"请输入您的宝贵意见!";
		_feedTextView.delegate = self;
	}
	return _feedTextView;
}

- (UIButton *)bottomButton {
	if (!_bottomButton) {
		_bottomButton = [UIButton buttonWithTitle:@"提交意见" color:COLOR_WITH_HEX(0x57524F) font:[UIFont boldSystemFontOfSize:kQKFitFont(16)]];
		[_bottomButton setBackgroundImage:[UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)] forState:UIControlStateNormal];
		[_bottomButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
	}
	return _bottomButton;
}


@end
